
var config = $.extend(true, {}, listApp, {
    data : {
        service : channelService,
        treeData:{},
        searchVm:{
            pageInfo:{
                orderField:"channel_code",
                order:"asc"
            }
        },
        tableVm:{
            loading:false
        },
        editVm:{
            editFormRules:{
                formRules:{
                    name : [ {required : true,message : '请输入通道名称',trigger : 'change'} ],
                    dataType : [ {required : true,message : '请选择类型',trigger : 'change'} ],
                    xishu:[
                        {
                            validator:function(rule, value, callback){
                                if (isNaN(value)) {
                                    callback(new Error('请输入数值'));
                                } else {
                                    callback();
                                }
                            },
                            trigger:'blur'
                        }],
                    historyData:[{required : true,message : '请确定是否上报历史数据',trigger : 'change'}],
                    alarmData:[{required : true,message : '请确定是否上报告警数据',trigger : 'change'}],
                }
            }
        },
        batchEditVm:{
            formName:"batchEditForm",
            data:{},
            visible : false,
            oprate : "指修改参数",
            multipleSelection:[],
            chk:{
                chkXishu:false,
                chkUnit:false,
                chkYuansu:false,
                chkHistoryData:false,
                chkAlarmData:false,
                chkMinMax:false,
                chkSwitch:false,
            },
            rules:{
                xishu:[{required : true,message : '请输入系数',trigger : 'change'},
                    {
                        validator:function(rule, value, callback){
                            var regPos = /^\d+(\.\d+)?$/; //非负浮点数
                            var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
                            if(regPos.test(value) || regNeg.test(value)){
                                callback();
                            }else{
                                callback(new Error('请输入数值'));
                            }
                        },
                        trigger:'blur'
                    }],
                unit:[{required : true,message : '请输入单位',trigger : 'change'}],
                yuansu:[{required : true,message : '请输入元素',trigger : 'change'}],
                historyData:[{required : true,message : '请确认历史数据包是否加入',trigger : 'change'}],
                alarmData:[{required : true,message : '请确认告警数据是否上报',trigger : 'change'}],
                min:[{required : true,message : '请填写模拟阈值',trigger : 'change'}],
                max:[{required : true,message : '请填写模拟阈值',trigger : 'change'}],
                norvalue:[{required : true,message : '请填写开关量常态',trigger : 'change'}],

            }
        },
        exchangeEditVm:{
            formName:"exchangeEditForm",
            data:{},
            visible : false,
            oprate : "通道置换",
            rules:{
                channelCode:[{required : true,message : '请填写置换通道号',trigger : 'change'}]
            },
            channelId:'',
        },
        translateEditVm:{
            formName:"exchangeEditForm",
            form:{
                data:[],
            },
            visible : false,
            oprate : "数值转译",
            channelId:'',
            rules:{
                key:[{required : true,message : '请输入采集数值',trigger : 'blur'},{
                    validator:function(rule, value, callback){
                        var regPos =/^[,|]$/
                        if(!regPos.test(value)){
                            callback();
                        }else{
                            callback(new Error('不能输入“，”,“|”'));
                        }
                    },
                    trigger:'blur'
                }],
                value:[{required : true,message : '请输入转译数值',trigger : 'blur'},{
                    validator:function(rule, value, callback){
                        var regPos =/^[,|]$/
                        if(!regPos.test(value)){
                            callback();
                        }else{
                            callback(new Error('不能输入“，”,“|”'));
                        }
                    },
                    trigger:'blur'
                }]
            },
            channelCode:'',
            channelName:''
        },
        treeStyle:{
            float:'left',
            width:'20%'
        },
        tableStyle:{
            float:'left',
            marginLeft:'20px',
            width:'78%'
        },
        spotList:[],
        upDataList:[{
            id:1,
            name:"是"
        },{
            id:0,
            name:"否"
        }],
        switchDataList:[{
            id:1,
            name:"开关量"
        },{
            id:0,
            name:"模拟量"
        }],
        multipleSelection:[],
        virtualList:[],
        virtualEditVm:{
            formName:"virtualForm",
            data:{},
            visible : false,
            oprate : "添加虚拟通道",
            rules:{
                channelCode:[{required : true,message : '请填写通道号',trigger : 'change'}],
                dataType:[{required : true,message : '请选择类型',trigger : 'change'}],
                virtualId:[{required : true,message : '请虚拟通道',trigger : 'change'}]
            },
            deviceId:'',
        },
        defaultLoad:false
    },
    filters:{
        formatUp:function(data){
            return data === 1 ? '是' : '否';
        },
        formatSwitch:function (data) {
            return data ===1 ? '开关量': (data ===0 ?'模拟量' : "")
        }
    },
    methods : {
        init:function () {
            var _this=this;
            // //加载spotList列表
            // spotService.getAllSpot("/getAllSpot",null,{
            //     success : function(ret) {
            //         _this.spotList=ret;
            //     },
            //     fail : function(res) {
            //         _this.showFailMsg(res.msg);
            //     }
            // });
        },
        getTreeSelectedVal:function (data) {
            this.treeData = data;
            // console.log('treeData',this.treeData);
            this.searchVm.deviceId=data.id;
            this.search();
        },
        collapse:function (isCollapsed) {

            if(isCollapsed){
                this.treeStyle.width='3%';
                this.tableStyle.width='90%';
            }else{
                this.treeStyle.width='20%';
                this.tableStyle.width='78%';
            }
        },
        buildCurrentSearch : function() {
            this.currentSearch = $.extend(true,{},this.searchVm);
            this.currentSearch.pageInfo.pageIndex=1;
            this.currentSearch.pageInfo.pageSize=this.tableVm.pageSize;
        },
        selectChannel:function (val) {
            this.multipleSelection = val;
            // console.log('selectChannel',this.multipleSelection);
        },
        change:function(prop,val){
            var _this=this;
            var arr=prop.split(",");
            if(val){
                if(arr.length>0){
                    arr.forEach(function (value) {
                        _this.$refs['batchEditForm'].validateField(value);
                    })
                }
            }else{
                if(arr.length>0){
                    arr.forEach(function (value) {
                        _this.$refs[value].resetField();
                    })
                }
            }
        },
        batchEdit:function () {
            if(this.multipleSelection == null || this.multipleSelection.length == 0){
                this.showFailMsg("请先选择需要批量修改的通道！");
                return false;
            }
            this.batchEditVm.data=$.extend(true,{},{});
            this.batchEditVm.chk.chkXishu=false;
            this.batchEditVm.chk.chkUnit=false;
            this.batchEditVm.chk.chkYuansu=false;
            this.batchEditVm.chk.chkHistoryData=false;
            this.batchEditVm.chk.chkAlarmData=false;
            this.batchEditVm.chk.chkMinMax=false;
            this.batchEditVm.chk.chkSwitch=false;


            this.batchEditVm.multipleSelection=this.multipleSelection;
            if(this.$refs.batchEditForm!=undefined)
                this.$refs.batchEditForm.resetFields();
            this.batchEditVm.visible = true;
        },
        saveBatchEdit:function(vm){
            var _this=this;
            // console.log('saveBatchEdit vm',vm);

            var newArr = vm.multipleSelection.map(function(item) {
                return item.id;
            })
            // console.log('newArr',newArr);

            var param={};

            if(vm.chk.chkXishu){
                if(vm.data.xishu!=undefined && vm.data.xishu!="")
                    param.xishu=vm.data.xishu;
                else{
                    _this.showFailMsg("请输入系数");
                    return false;
                }
            }

            if(vm.chk.chkUnit){
                if(vm.data.unit!=undefined && vm.data.unit!="")
                    param.unit=vm.data.unit;
                else{
                    _this.showFailMsg("请输入单位");
                    return false;
                }
            }

            if(vm.chk.chkYuansu){
                if(vm.data.yuansu!=undefined && vm.data.yuansu!="")
                    param.yuansu=vm.data.yuansu;
                else{
                    _this.showFailMsg("请输入元素");
                    return false;
                }
            }

            if(vm.chk.chkHistoryData){
                if(vm.data.historyData!=undefined)
                    param.historyData=vm.data.historyData;
                else{
                    _this.showFailMsg("请确认历史数据包是否加入");
                    return false;
                }
            }

            if(vm.chk.chkAlarmData){
                if(vm.data.alarmData!=undefined)
                    param.alarmData=vm.data.alarmData;
                else{
                    _this.showFailMsg("请确认告警数据是否上报");
                    return false;
                }
            }

            if(vm.chk.chkMinMax){
                if(vm.data.min!=undefined && vm.data.min!="" && vm.data.max!=undefined && vm.data.max!=""){
                    param.min=vm.data.min;
                    param.max=vm.data.max;
                }
                else{
                    _this.showFailMsg("请填写模拟阈值");
                    return false;
                }

            }
            if(vm.chk.chkSwitch){
                if(vm.data.norvalue!=undefined && vm.data.norvalue!="")
                    param.norvalue=vm.data.norvalue;
                else{
                    _this.showFailMsg("请填写开关量常态");
                    return false;
                }
            }

            param.ids=newArr;

            _this.service.batchSaveChannel("/batchSaveChannel",param,{
                success:function (res) {
                    vm.loading = false;
                    _this.search();
                    vm.visible = false;
                },
                fail:function (ret) {
                    vm.loading = false;
                    _this.showFailMsg(ret.msg)
                }
            })



        },
        exchange:function (index, row) {
            this.exchangeEditVm.visible = true;
            this.exchangeEditVm.data = $.extend(true,{},row);

        },
        saveExchane:function(vm){
            // console.log('saveExchane vm',vm);
            var _this=this;
            this.service.exchangeChannel("/exchangeChannel",vm.data,{
                success:function (res) {
                    vm.loading = false;
                    _this.search();
                    vm.visible = false;
                },
                fail:function (ret) {
                    vm.loading = false;
                    _this.showFailMsg(ret.msg);
                }
            });
        },
        translate:function (index,row) {
            console.log(row);
            var _this=this;
            _this.translateEditVm.channelCode=row.channelCode;
            _this.translateEditVm.channelName=row.name;
            _this.translateEditVm.form.data=[];
            this.translateEditVm.channelId=row.id;
            var items=row.translate.split(',');
            if(row.translate.length>0 && items.length>0){
                items.forEach(function(val){
                    // console.log(val+"\n")
                    var vals=val.split('|');
                    _this.translateEditVm.form.data.push({
                        isEdit:false,
                        key:vals[0],
                        value:vals[1]
                    });
                })
            }

            this.translateEditVm.visible = true;

        },
        addTranslate:function () {
            this.translateEditVm.form.data.push({
                isEdit:true,
                key:'',
                value:''
            });
        },
        saveTranslate:function (index,row) {
            if(row==undefined || row.key==undefined || row.value == undefined || row.key=="" || row.value==""){
                this.showFailMsg("采集数值或转译数值未填写！");
                return false;
            }
            if(row!=undefined && row.key!=undefined && (row.key.indexOf(",")!=-1 || row.key.indexOf("|") !=-1)){
                this.showFailMsg("采集数值不能包含“,”,“|”！");
                return false;
            }
            if(row!=undefined && row.value!=undefined && (row.value.indexOf(",")!=-1 || row.value.indexOf("|") !=-1)){
                this.showFailMsg("译数值不能包含“,”,“|”！");
                return false;
            }

            row.isEdit=false;
            this.saveTranslateVal(index,row);
        },
        editTranslate:function (index,row) {
            row.isEdit=true;
        },
        deleteTranslate:function (index,row) {
            this.translateEditVm.form.data.splice(index, 1);
            this.saveTranslateVal(index,row);
        },
        saveTranslateVal:function (index,row) {
            var _this=this;
            if(row==undefined || row.key==undefined || row.value == undefined || row.key=="" || row.value=="")
                return false;

            // console.log('channelId',this.translateEditVm.channelId,this.translateEditVm.data);

            var strTranslateVal="";
            var valArr=[];
            this.translateEditVm.form.data.forEach(function (val) {
                valArr.push(val.key.concat('|').concat(val.value));
            })
            strTranslateVal=valArr.join(',');
            // console.log('strTranslateVal=',strTranslateVal);

            var param={
                id:this.translateEditVm.channelId,
                translate:strTranslateVal
            }
            this.service.updateTranslate("/updateTranslate",param,{
                success:function (ret) {
                    _this.search();
                },
                fail:function (res) {
                    _this.showFailMsg(res.msg);
                    row.isEdit=true;
                }
            })


        },
        renderTranslateHeader:function (h, { column, $index }) {
            // console.log('renderTranslateHeader',column)
            return h('span', { slot: 'reference',},[
                h('span', {style:'color:red;'}, '*'),
                h('span', {}, column.label),
            ])
        },
        formatterSwitch:function (row) {
            return row.dataType == 1 ? row.norvalue : ""
        },
        formatterMin:function (row) {
            return row.dataType == 0 ? row.min : ""
        },
        formatterMax:function (row) {
            return row.dataType == 0 ? row.max : ""
        },
        clearChannel:function () {
            //清空列表
            if(this.treeData== null || this.treeData.id == null || this.treeData.id==""){
                this.showFailMsg("请先选择设备！");
                return false;
            }
            var _this=this;

            this.$confirm('此操作将删除设备下所有通道, 是否继续?', '提示', {
                confirmButtonText : '确定',
                cancelButtonText : '取消',
                type : 'warning'
            }).then(function() {
                _this.service.clearChannelByDeviceId("/clear/"+_this.treeData.id,null,{
                    success:function (res) {
                        _this.showSuccessMsg("清空列表成功");
                        _this.search();
                    },
                    fail:function (ret) {
                        _this.showFailMsg(ret.msg);
                    }
                })
            });


        },
        // addVirtual:function () {
        //     if(this.treeData== null || this.treeData.id == null || this.treeData.id==""){
        //         this.showFailMsg("请先选择设备！");
        //         return false;
        //     }
        //     this.virtualEditVm.data = $.extend(true,{},{});
        //     this.virtualEditVm.deviceId=this.treeData.id;
        //     this.virtualEditVm.visible = true;
        // },
        // saveVirtual:function (vm) {
        //     console.log('saveVirtual',vm);
        //     var _this=this;
        //     this.$refs[vm.formName].validate(function(valid) {
        //         if(valid){
        //             var param=$.extend(true,{},vm.data);
        //             param.deviceId=vm.deviceId;
        //             _this.service.addVirtual("/addVirtual",param,{
        //                 success:function (ret) {
        //                     vm.loading = false;
        //                     _this.search();
        //                     vm.visible = false;
        //                 },
        //                 fail:function (res) {
        //                     vm.loading = false;
        //                     _this.showFailMsg(res.msg);
        //                 }
        //             });
        //         }else{
        //             _this.showFailMsg('校验不通过哦');
        //             return false;
        //         }
        //     });
        // },
        initPull:function () {
            if(this.treeData== null || this.treeData.id == null || this.treeData.id==""){
                this.showFailMsg("请先选择设备！");
                return false;
            }
            var _this=this;
            _this.service.addVirtual("/initPull/"+_this.treeData.id,null,{
                success:function (ret) {
                    _this.search();
                },
                fail:function (res) {
                    _this.showFailMsg(res.msg);
                }
            });
        },
        reduction:function () {
            var _this=this;
            if(this.treeData== null || this.treeData.id == null || this.treeData.id==""){
                this.showFailMsg("请先选择设备！");
                return false;
            }

            this.$confirm('此操作将还原设备下所有通道名, 是否继续?', '提示', {
                confirmButtonText : '确定',
                cancelButtonText : '取消',
                type : 'warning'
            }).then(function() {
                _this.service.reductionChannel("/reductionChannelName/"+_this.treeData.id,null, {
                    success : function(ret) {
                        _this.search();
                    },
                    fail : function(res) {
                        _this.showFailMsg(res.msg);
                    }
                });
            });
        },
        rowClass:function ({row,rowIndex}) {

            if (row.virtualId !=null && row.virtualId != undefined && row.virtualId!="") {
                return 'virtual-row';
            } else  {
                return '';
            }
        },
        handleSizeChange:function (val) {
            this.tableVm.pageSize=val;
            this.search();
        },
        search : function() {
            if(this.treeData==null || this.treeData == undefined || this.treeData.id==undefined){
                this.showFailMsg("请先选择设备再查询");
                return false;
            }
            this.buildCurrentSearch();
            this.requsetPage();
        },
        showSuccessMsg:function(msg){
            this.$message({
                message : msg,
                type : 'success',
                customClass:'msg'
            });
        },
        showFailMsg:function(msg){
            this.$message({
                message : msg,
                type : 'fail',
                customClass:'msg'
            });
        }

    }
});
var app = new Vue(config);